package com.mcflywzx.airhockey.objects;

import static android.opengl.GLES20.GL_TRIANGLE_FAN;
import static android.opengl.GLES20.glDrawArrays;
import static android.opengl.GLES20.glDeleteTextures;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;

import com.mcflywzx.airhockey.Constants;
import com.mcflywzx.airhockey.data.VertexArray;
import com.mcflywzx.airhockey.programs.TextureShaderProgram;
import com.mcflywzx.airhockey.util.TextureHelper;
import com.mcflywzx.airhockey.programs.*;
import com.mcflywzx.airhockey.util.Geometry.*;
import android.content.*;

public class MSquare {
	
	private static final int POSITION_COMPONENT_COUNT = 3; //一个顶点有两个分量
	private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
	private static final int STRIDE = (POSITION_COMPONENT_COUNT + TEXTURE_COORDINATES_COMPONENT_COUNT) * Constants.BYTES_PER_FLOAT;
	
	private final VertexArray va;
	
	public int tex = 0;
	
	final TextureShaderProgram tsp;
	
	final ColorShaderProgram csp;
	
	private final Context co;
	 
	private Point p;
	
	private Vector color;
	
	public MSquare(Point p, float w, float h, TextureShaderProgram tsp, Context co, int resourceId, boolean isN) {
		this.p = p;
		this.co = co;
		this.tsp = tsp;
		this.csp = null;
		getTexture(resourceId, isN);
		va = new VertexArray(new float[] {
				//X,Y,Z,S,T
				p.x, p.y, p.z, 0.5f, 0.5f,
				p.x - (w/2), p.y, p.z -(h/2), 0f, 1f,
				p.x + w/2, p.y, p.z - (h/2), 1f, 1f,
				p.x + w/2, p.y, p.z + h/2, 1f, 0f,
				p.x - (w/2), p.y, p.z + h/2, 0f, 0f,
				p.x - (w/2), p.y, p.z - (h/2), 0f, 1f
		});
	}
	
	public MSquare(Point p, float w, float h, Vector color, ColorShaderProgram csp) {
		this.p = p;
		this.csp = csp;
		this.color = color;
		this.tsp = null;
		this.co = null;
		//setTextTex(showText);
		va = new VertexArray(new float[] {
								 //X,Y,S,T
								 p.x, p.y, p.z,
								 p.x - (w/2), p.y, p.z -(h/2),
								 p.x + w/2, p.y, p.z - (h/2),
								 p.x + w/2, p.y, p.z + h/2,
								 p.x - (w/2), p.y, p.z + h/2,
								 p.x - (w/2), p.y, p.z - (h/2)
							 });
	}
	
	public void bindData(ColorShaderProgram cP) {
		va.setVertexAttribPointer(0, cP.getPositionAttributeLocation(), POSITION_COMPONENT_COUNT, 0);
	}
	
	public void bindData(TextureShaderProgram tsp) {
		va.setVertexAttribPointer(
				0,
				tsp.getPositionAttributeLocation(),
				POSITION_COMPONENT_COUNT,
				STRIDE);
		
		va.setVertexAttribPointer(
				POSITION_COMPONENT_COUNT,
				tsp.getTextureCoordinatesAttributeLocation(),
				TEXTURE_COORDINATES_COMPONENT_COUNT, 
				STRIDE);
	}
	
	public int getTexture(int resourceId, boolean isN) {
		//Delet useless OpenGL texture resource
		if(tex != 0) {
			int[] Dtex = new int[1];
			Dtex[0] = tex;
			glDeleteTextures(1, Dtex, 0);
		}
		
		
		tex = TextureHelper.loadTexture(co, resourceId, isN);
		
		return tex;

	}
	
	public void draw(float[] matrix) {
		//PositionAndRotateObjectInScene(0f, 0.5f, 0f);
		if(tsp == null){
			//PositionAndRotateObjectInScene(0f, mallet.H / 2f, -0.4f);
			csp.useProgram();
			csp.setUniforms(matrix, color.x, color.y, color.z);
			bindData(csp);
		} else if(csp == null){
			tsp.useProgram();
			tsp.setUniforms(matrix, tex);
			bindData(tsp);
		}
		
		glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
	}
}

